<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
  
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
</head>
<body>
    
    <div id="app">

            <div class="panel panel-primary">
                    <div class="panel-heading">
                          <h3 class="panel-title">添加品牌</h3>
                    </div>
                    <div class="panel-body form-inline">
                          <label>Name:</label>
                          <input type="text" v-model="name" class="form-control" />
                          <button v-on:click="add">添加</button>
                    </div>
              </div>

        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime|format_timtstarmp}}</td>
                    <td><a href="javascript:void(0)" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        Vue.http.options.root = 'http://localhost:5000/';
        var vm = new Vue({
            el: '#app',
            data: {
                name:'',
                list: [
                    {id:1, name:'奔驰', ctime: new Date()},
                    {id:2, name:'宝马', ctime: new Date()}                    
                ]
            },
            methods: {                
                getAllList:function(){
                    // 由于已经导入了Vue-resource这个包，所以可以直接通过 this.$http 发起数据请求
                    this.$http.get('prod').then(result=>{
                        if (result.status == 200){
                            this.list = result.body;
                        }
                    });
                },
                add: function(){
                    this.$http.post('prod', {name: this.name}, {emulateJSON: true}).then(result=>{
                        if(result.status == 200){
                            this.getAllList();
                        }else{
                            alert('添加失败')
                        }
                    });
                },
                del(id){
                    this.$http.post('prod/'+ id , {}, {emulateJSON: true}).then(result=>{
                        if(result.status == 200){
                            this.getAllList();
                        }else{
                            alert('删除失败');
                        }
                    })
                }
            },
            filters:{
                format_timtstarmp(starmp){
                    // 将时间戳转为日期时间字符串
                    starmp = starmp * 1000;
                    var time = new Date(starmp);
                    var year = time.getFullYear();
                    var month = time.getMonth()+1;
                    var date = time.getDate();
                    var hours = time.getHours();
                    var minutes = time.getMinutes();
                    var seconds = time.getSeconds();
                    return year+'-'+(month)+'-'+(date)+' '+(hours)+':'+(minutes)+':'+(seconds);
                }
            },
            created(){
                this.getAllList(); 
            }
        });
    </script>
</body>
</html>